package logic

import (
	"context"
	"src/models/mysql"
	"src/srcclient"

	"src/internal/svc"
	"src/src"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodListLogic {
	return &GoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodListLogic) GoodList(in *src.GoodListRequest) (*src.GoodListResponse, error) {
	// todo: add your logic here and delete this line

	var list []mysql.Good
	mysql.DB.Debug().Model(&list).Where("status like ?", "%"+in.Status+"%").Order("stock  desc").Find(&list)

	var slice []*srcclient.GoodListStruct

	for _, good := range list {
		lists := srcclient.GoodListStruct{
			Title:       good.Title,
			Price:       int64(good.Price),
			Stock:       int64(good.Stock),
			Status:      good.Status,
			LikeCollect: int64(good.LikeCollect),
		}
		slice = append(slice, &lists)
	}

	return &src.GoodListResponse{GoodList: slice}, nil
}
